package util 
{
	import flash.display.DisplayObject;
	import flash.filters.ColorMatrixFilter;
	/**
	 * ...
	 * @author kasoi
	 */
	public class FilterMaster
	{
		
		static public function blackAndWhite(image: DisplayObject, value: Number = 1) : void 
		{
			var id: Number = -0.001;
			
			var rLum:Number = 0.2225 * value;
			var gLum:Number = 0.7169 * value;
			var bLum:Number = 0.0606 * value;
			
			var r1: Number = 1 - (1 - 0.225) * value;
			var g2: Number = 1 - (1 - 0.7169) * value;
			var b3: Number = 1 - (1 - 0.0606) * value;
			
			var bwMatrix: Array = 
				[r1, gLum, bLum, 0, 0,
				rLum, g2, bLum, 0, 0,
				rLum, gLum, b3, 0, 0,
				0, 0, 0, 1, id];
			
			var filter: ColorMatrixFilter = new ColorMatrixFilter(bwMatrix);
			
			var contain: Boolean = false;
			var filters: Array = image.filters;
			for (var i: int = 0; i < filters.length; i++) 
			{
				var _filter: ColorMatrixFilter = filters[i] as ColorMatrixFilter;
				if (_filter != null)
				{
					var matrix: Array = _filter.matrix;
					var num: Number = matrix[matrix.length - 1];
					num = Number(num.toFixed(4));
					if (num == id) 
					{
						contain = true;
						filters.splice(i, 1, filter);
					}
				}
			}
			
			if (!contain) filters.push(filter);
			
			image.filters = filters;
		}
		
		static public function brightness(image: DisplayObject, value: Number = 0.2) : void 
		{
			var id: Number = -0.002;
			
			var bright: Number = 100 * value;
			
			var bMatrix: Array = 
				[1, 0, 0, 0, bright,
				0, 1, 0, 0, bright,
				0, 0, 1, 0, bright,
				0, 0, 0, 1, id];
			
			var filter: ColorMatrixFilter = new ColorMatrixFilter(bMatrix);
			
			var contain: Boolean = false;
			var filters: Array = image.filters;
			for (var i: int = 0; i < filters.length; i++) 
			{
				var _filter: ColorMatrixFilter = filters[i] as ColorMatrixFilter;
				if (_filter != null)
				{
					var matrix: Array = _filter.matrix;
					var num: Number = matrix[matrix.length - 1];
					num = Number(num.toFixed(4));
					if (num == id) 
					{
						contain = true;
						filters.splice(i, 1, filter);
					}
				}
			}
			
			if (!contain) filters.push(filter);
			
			image.filters = filters;
		}
		
	}

}